package com.example.demo.util;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

/**
 * webservice客户端调用工具类
 *
 * @author chenzhigang
 */
public class WebserviceUtil {

    /**
     * 执行webservice服务
     *
     * @param wsdlUrl wsdl地址
     * @param method 方法名称
     * @param objects 参数集合
     * @return 返回结果
     * @throws Exception 异常
     */
    public static String execute(String wsdlUrl, String method, Object... objects) throws Exception {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdlUrl);
        Object[] resultObj = client.invoke(method, objects);
        return resultObj[0].toString();
    }

    /**
     * 设置超时时间
     *
     * @param client 客服端对象
     * @param timeout 超时时间（毫秒）
     */
    private static void setTimeout(Client client, long timeout) {
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(timeout);
        policy.setReceiveTimeout(timeout);
        conduit.setClient(policy);
    }


    public static void main(String[] args) throws Exception {
//        Object[] objects = new Object[] {"KX09090002", "R0001-00", "T0001-00", "YIIvMkzPXD"};
//        String resultXml = execute("http://117.74.136.117:6161/WebTheory.asmx?wsdl", "pupilInfo", objects);
//        System.out.println("resultXml:" + resultXml);
//        Object[] objects = new Object[] {"KX09090002", "R0001-00", "T0001-00", "YIIvMkzPXD"};
//        String resultXml = execute("http://117.74.136.117:6161/WebTheory.asmx?wsdl", "importTuiTheory", objects);
//        System.out.println("resultXml:" + resultXml);

//        WsJGClassrecordReq wsJGClassrecordReq = new WsJGClassrecordReq();
//        wsJGClassrecordReq.setCarNum("123");
//        String xmlReq = XmlParseUtil.beanToXml(wsJGClassrecordReq);
        String xmlReq = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
                "<root>\n" +
                "    <BOSID>WWE2021111610565829078333521a4ab2a4</BOSID>\n" +
                "    <STUDENTCODE>KX09090002</STUDENTCODE>\n" +
                "    <SCHOOLCODE>R0001-00</SCHOOLCODE>\n" +
                "    <STARTTIME>20211110134644</STARTTIME>\n" +
                "    <ENDTIME>20211110134649</ENDTIME>\n" +
                "    <TOTALTIME>10</TOTALTIME>\n" +
                "    <TOATLTENOR>0.0</TOATLTENOR>\n" +
                "    <PERPRICE>0</PERPRICE>\n" +
                "    <TOTALMONEY>0</TOTALMONEY>\n" +
                "    <TRAINCODE>km01</TRAINCODE>\n" +
                "    <EXAMCODE>km01</EXAMCODE>\n" +
                "    <STUCARDCODE>0</STUCARDCODE>\n" +
                "    <POSCODE>0</POSCODE>\n" +
                "    <CARNUM>0</CARNUM>\n" +
                "    <READTYPE>0</READTYPE>\n" +
                "    <ISVALID>1</ISVALID>\n" +
                "    <ISCHECKED>0</ISCHECKED>\n" +
                "    <ISCOACHECKED>0</ISCOACHECKED>\n" +
                "    <ISFORCED>0</ISFORCED>\n" +
                "    <FORCEDTIME>0</FORCEDTIME>\n" +
                "    <ISCONFIRM>0</ISCONFIRM>\n" +
                "    <CONFIRMTIME>0</CONFIRMTIME>\n" +
                "    <COLLECTOR>0</COLLECTOR>\n" +
                "    <COLLECTIME>2021-11-10 13:46:44</COLLECTIME>\n" +
                "    <SOURCE>0</SOURCE>\n" +
                "    <ACCOUNTFLAG>0</ACCOUNTFLAG>\n" +
                "    <ACCOUNTIME>1313</ACCOUNTIME>\n" +
                "    <UWREMARK>0</UWREMARK>\n" +
                "    <REISSUECNT>0</REISSUECNT>\n" +
                "    <TRAINTYPECOUNT>0</TRAINTYPECOUNT>\n" +
                "    <STOPTIMES>0</STOPTIMES>\n" +
                "    <OPERTIME>0</OPERTIME>\n" +
                "    <DATABASETIME>2021-11-10 13:46:44</DATABASETIME>\n" +
                "    <IP>0</IP>\n" +
                "    <PUPILFACE>1</PUPILFACE>\n" +
                "    <COACHFACE>0</COACHFACE>\n" +
                "    <TRAINPHOTO>0</TRAINPHOTO>\n" +
                "    <ISCHECKEDOUT>0</ISCHECKEDOUT>\n" +
                "    <ISCOACHECKEDOUT>0</ISCOACHECKEDOUT>\n" +
                "    <PUPILFACEOUT>1</PUPILFACEOUT>\n" +
                "    <COACHFACEOUT>0</COACHFACEOUT>\n" +
                "   <CONNUSER>T0001-00</CONNUSER>\n" +
                "   <CONNMIMA>YIIvMkzPXD</CONNMIMA>\n" +
                "</root>";
        System.out.println(xmlReq);
        // 设置本地代理到fiddler
        System.setProperty("http.proxySet", "true");
        System.setProperty("http.proxyHost", "127.0.0.1");
        System.setProperty("http.proxyPort", "8888");
        String resultXml = execute("http://117.74.136.117:6161/WebTheory.asmx?wsdl", "importTuiTheory", xmlReq);
        System.out.println(resultXml);

//        XmlParseUtil.beanToXml(WsJGClassrecordReq)

    }

}
